{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import warnings\n",
    "warnings.filterwarnings('ignore')\n",
    "import numpy as np\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "构建重要性矩阵、评分差值矩阵、成对比较矩阵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1.        , 3.        , 2.        , 9.        ],\n",
       "       [0.33333333, 1.        , 0.5       , 7.        ],\n",
       "       [0.5       , 2.        , 1.        , 8.        ],\n",
       "       [0.11111111, 0.14285714, 0.125     , 1.        ]])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "array = np.array([9.8, 7.5, 8.5, 1]) # 重要性取值\n",
    "tran_array = array.reshape(-1,1) # 转置\n",
    "diff_array = tran_array - array # 评分差值矩阵\n",
    "# 构建成对比较矩阵\n",
    "matrix = []\n",
    "for row in diff_array:\n",
    "    arr_matrix = []\n",
    "    for i in row:\n",
    "        if i >= 0:\n",
    "            arr_matrix.append(int(i)+1)\n",
    "        else:\n",
    "            arr_matrix.append(1/(-int(i)+1))\n",
    "    matrix.append(arr_matrix)\n",
    "matrix = np.array(matrix)\n",
    "matrix"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "计算列归一矩阵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.51428571, 0.48837209, 0.55172414, 0.36      ],\n",
       "       [0.17142857, 0.1627907 , 0.13793103, 0.28      ],\n",
       "       [0.25714286, 0.3255814 , 0.27586207, 0.32      ],\n",
       "       [0.05714286, 0.02325581, 0.03448276, 0.04      ]])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tran_matrix = matrix.T\n",
    "scaler_matrix = []\n",
    "for row in tran_matrix:\n",
    "    row_sum = row.sum()\n",
    "    arr_matrix = []\n",
    "    for i in row:\n",
    "        arr_matrix.append(i / row_sum)\n",
    "    scaler_matrix.append(arr_matrix)\n",
    "\n",
    "scaler_matrix = np.array(scaler_matrix).T\n",
    "scaler_matrix"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "AHP构建与检验"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "通过一致性检验\n"
     ]
    }
   ],
   "source": [
    "def ri_index(n):\n",
    "    if n == 1 or n == 2:\n",
    "        return 0\n",
    "    elif n == 3:\n",
    "        return 0.58\n",
    "    elif n == 4:\n",
    "        return 0.9\n",
    "    elif n == 5:\n",
    "        return 1.12\n",
    "    elif n == 6:\n",
    "        return 1.24\n",
    "    elif n == 7:\n",
    "        return 1.32\n",
    "    elif n == 8:\n",
    "        return 1.41\n",
    "    elif n == 9:\n",
    "        return 1.45\n",
    "    elif n == 10:\n",
    "        return 1.49\n",
    "    elif n == 11:\n",
    "        return 1.51\n",
    "    elif n == 12:\n",
    "        return 1.54\n",
    "    elif n == 13:\n",
    "        return 1.56\n",
    "    elif n == 14:\n",
    "        return 1.57\n",
    "    else:\n",
    "        pass\n",
    "           \n",
    "# 计算权重向量\n",
    "weight = []\n",
    "n = len(scaler_matrix)\n",
    "for row in  scaler_matrix:\n",
    "    weight.append(sum(row) / n)\n",
    "weight = np.array(weight).reshape(-1,1)\n",
    "\n",
    "# 计算BW\n",
    "weight_b = []\n",
    "for row in matrix:\n",
    "    weight_b.append(sum(np.dot(row, weight)))\n",
    "weight_b = np.array(weight_b).reshape(-1,1)\n",
    "\n",
    "# 计算BW/W\n",
    "bw_w = weight_b / weight\n",
    "\n",
    "# 计算最大特征根λ\n",
    "max_lambda = sum(bw_w) / n\n",
    "\n",
    "# CI一致性指标\n",
    "ci = (max_lambda - n) / (n - 1)\n",
    "ri = ri_index(n)\n",
    "cr = (ci / ri)\n",
    "if cr < 0.1:\n",
    "    print('通过一致性检验')\n",
    "else:\n",
    "    print('不通过一致性检验')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
